import requests


def get_status():
    # res = requests.get("http://127.0.0.1:5000/a")
    res = requests.get("http://127.0.0.1:5000")
    # print(type(res.status_code))
    if res.status_code == 200:
        print(res.text)
    else:
        print(res.status_code)


def get_response():
    # res = requests.get("http://127.0.0.1:5000/a")
    res = requests.get("http://127.0.0.1:5000/response_type")
    # print(type(res.status_code))
    if res.status_code == 200:
        # print(res.text)
        # print(res.content.decode("GBK"))
        print(res.json())
        print(res.json()['info'])
        print(res.headers['Content-Type'])

        print(res.headers['Content-Type'], res.encoding)
    else:
        print(res.status_code)


def get_params():
    res = requests.get("http://127.0.0.1:5000/params?page=1&size=10", params={
        "type": "views",
        "sort": "id",
        "limit": 50
    })
    print(res.json())


def get_data():
    res = requests.post("http://127.0.0.1:5000/data", data={
        "admin": "admin",
        "password": "123456"
    })
    print(res.request.headers["Content-Type"], res.json())


# get_data()


def get_json():
    res = requests.post("http://127.0.0.1:5000/data", json={
        "account": "admin",
        "password": "123456"
    })
    print(res.request.headers["Content-Type"], res.json())


# get_json()

def set_headers():
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
        "Referer": "http://127.0.0.1:5000"
    }
    res = requests.get("http://127.0.0.1:5000/headers", headers=headers)
    if res.status_code == 200:
        print(res.json())
    else:
        print(res)


# set_headers()

def login():
    res = requests.post("http://127.0.0.1:5000/login", json={
        "username": "admin",
        "password": "123456"
    })
    # print(res.json(), res.cookies)
    # for c in res.cookies:
    #     print(c.name)
    return res.cookies


# login()


def get_center():
    headers = {
        # "Cookie": "username=zzy"
    }
    # cookies = {
    #     "username": "zzy",
    #     "age": "20"
    # }
    cookies = login()
    res = requests.get("http://127.0.0.1:5000/center", headers=headers, cookies=cookies)
    # res = requests.get("http://127.0.0.1:5000/center", headers=headers)

    if res.status_code == 200:
        print(res.json())
    else:
        print(res)


# get_center()


def login_get_user_info():
    # res = requests.post("http://127.0.0.1:5000/login", json={
    #     "username": "admin",
    #     "password": "123456"
    # })
    # cookies = res.cookies
    # res = requests.get("http://127.0.0.1:5000/center", cookies=cookies)
    # print(res.json())

    # 构建会话实例，实现状态保持
    session = requests.Session()
    session.post("http://127.0.0.1:5000/login", json={
        "username": "admin",
        "password": "123456"
    })
    print(session.cookies)
    session.get("http://127.0.0.1:5000/center")
    print(session)


login_get_user_info()
